package generator.model;

import generator.model.generatorEntities.Supplement;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SupplementGenerator extends AbstractGenerator {

	private List<Supplement> supplements;
	
	private Supplement currentSupplement;

	public SupplementGenerator() {
		supplements = new ArrayList<Supplement>();
		
		Supplement sofiesMode = new Supplement("Sofies Mode", null);
		sofiesMode.addNewsLine("Bikinichock!");
		sofiesMode.addNewsLine("Är du supertjock?");
		sofiesMode.addNewsLine("Nya kalsong-på-huvudet-trenden!");
		supplements.add(sofiesMode);
		
		Supplement sondags = new Supplement("Söndagsbilagan", null);
		sondags.addNewsLine("Så älskar svenska folket!");
		sondags.addNewsLine("Exklusiv karta över kvinnokroppen - riv ut och spara!");
		sondags.addNewsLine("Så får du din man att sluta hata din mamma!");
		supplements.add(sondags);
		
		this.currentSupplement = sofiesMode;
	}
	
	public SupplementGenerator(String filename) {
		
		supplements = new ArrayList<Supplement>();
		
		try {
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename));
			
			HSSFSheet s = wb.getSheet("Bilagor");
			
			HSSFRow names = s.getRow(0);
			HSSFRow pictures = s.getRow(1);
		
			
			for(int i=0; i<=names.getLastCellNum(); i++) {
				HSSFCell name = names.getCell(i);
				HSSFCell picture =  pictures.getCell(i);
				
				if(name != null && picture != null)
					supplements.add(new Supplement(name.getStringCellValue(), picture.getStringCellValue()));
			}
			
			//All newslines for each supplement
			for(int i=2; i<=s.getLastRowNum(); i++) {
				HSSFRow row = s.getRow(i);
				for(int j=row.getFirstCellNum(); j<=row.getLastCellNum(); j++) {
					HSSFCell cell = row.getCell(j);
					if(cell != null) {
						supplements.get(j).addNewsLine(cell.getStringCellValue());
					}
				}
				
			}
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void randomizeSupplement() {
		Random r = new Random();
		this.currentSupplement = supplements.get(r.nextInt(supplements.size()));
	}
	
	public String getTagLine() {
		
		return this.currentSupplement.getSupplement();
	}
	public String getSupplementName() {
		
		return this.currentSupplement.getName();
	}
	public String getSupplementPicture() {
		
		return this.currentSupplement.getPicture();
	}
}
